package com.tongfusoft.document.web.controller.administration;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.document.entity.AccountHasUsers;
import com.tongfusoft.document.entity.RoleHasUsers;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.document.manager.user.service.IDocumentUserService;
import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.spring.web.core.controller.BaseController;
import com.tongfusoft.spring.web.core.controller.annotation.UrlDesc;
import com.tongfusoft.spring.web.core.data.DataTable;
import com.tongfusoft.spring.web.core.data.ResultModel;

@RequestMapping("/manager")
@Controller
public class UserController extends BaseController {

	@Autowired
	@Qualifier(value = "DocumentUserService")
	public IDocumentUserService userService;

	@UrlDesc(value = "用户信息列表页")
	@RequestMapping(value ="/user/listPage")
	public String userList(HttpServletRequest request, Model model) {

		model.addAttribute("mainPageKey", "userList");
		model.addAttribute("mainPage", "manager/user/userList");

		return "base";
	}

	@UrlDesc(value = "添加用户页")
	@RequestMapping(value ="/user/addPage")
	public String addUser(HttpServletRequest request, Model model) {

		model.addAttribute("mainPageKey", "addUser");
		model.addAttribute("mainPage", "manager/user/addUser");

		return "base";
	}
	
	@UrlDesc(value = "用户编辑页")
	@RequestMapping(value ="/user/editPage")
	public String updateUser(HttpServletRequest request, Model model) {
		String userId = request.getParameter("userId");
		String opr = request.getParameter("opr");

		model.addAttribute("opr", opr);
		model.addAttribute("userId", userId);
		model.addAttribute("mainPageKey", "updateUser");
		model.addAttribute("mainPage", "manager/user/updateUser");

		return "base";
	}
	
	@UrlDesc(value = "用户详情页")
	@RequestMapping("/user/detailPage/{userId}")
	public String userDetail(@PathVariable("userId") String userId, Model model) {
		model.addAttribute("userId", userId);
		model.addAttribute("mainPageKey", "detailUser");
		model.addAttribute("mainPage", "manager/user/detailUser");
		return "base";
	}

	@UrlDesc(value = "用户详情数据获取")
	@RequestMapping(value ="/user/detail/{userId}")
	@ResponseBody
	public ResultModel detailUser(@PathVariable("userId") String userId) {
		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			rm.setData(this.userService.getUserById(userId));
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;

	}

	@UrlDesc(value = "用户信息列表页数据获取")
	@RequestMapping(value="/user/query")
	@ResponseBody
	public DataTable<Users> userQuery(Model model, int length, int start,
			int draw, String userId, String order) {
		DataTable rm = new DataTable();
		try {
			Users users = new Users();
			int pageIndex = (start / length);
			PageInfo pageInfo = this.userService.usersPageTable(pageIndex,length, users);

			rm.setDraw(draw);
			rm.setRecordsFiltered(pageInfo.getTotal());
			rm.setRecordsTotal(pageInfo.getTotal());
			rm.setData(pageInfo.getList());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rm;
	}

	@UrlDesc(value = "添加用户")
	@RequestMapping(value ="/user/add")
	@ResponseBody
	public ResultModel userAdd(Users user, Model model) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			rm.setData(this.userService.addUser(user, (Users) super.getUser()));
		} catch (ServiceException se) {
			rm.setCode(se.getMessage());
			rm.setState("error");
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}

	@UrlDesc(value = "删除用户")
	@RequestMapping(value ="/user/del")
	@ResponseBody
	public ResultModel userDel(Users user) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			this.userService.delUser(user, (Users) super.getUser());
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}

	@UrlDesc(value = "当前登录用户编辑")
	@RequestMapping(value ="/user/currentUserEdit")
	@ResponseBody
	public ResultModel userSeflEdit(Users user) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			this.userService.updateUser(user, (Users) super.getUser());
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;

	}

	@UrlDesc(value = "用户编辑")
	@RequestMapping(value ="/user/edit")
	@ResponseBody
	public ResultModel userEdit(Users user) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			this.userService.updateUserWithoutPwd(user, (Users) super.getUser());
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}

	@UrlDesc(value = "重置密码")
	@RequestMapping(value ="/user/rePassword")
	@ResponseBody
	public ResultModel rePassword(HttpServletRequest request, Users user) {

		ResultModel rm = new ResultModel();
		rm.setState("success");
		try {
			Users newUser = this.userService.getUserById(user.getUserId());
			this.userService.rePassword(newUser, (Users) super.getUser());
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;

	}

	@UrlDesc(value = "添加用户与角色权限")
	@RequestMapping(value ="/roleHasUsers/add")
	@ResponseBody
	public ResultModel roleHasUsersAdd(HttpServletRequest request, Model model) {

		ResultModel rm = new ResultModel();
		rm.setState("success");
		RoleHasUsers ru = new RoleHasUsers();
		try {
			String userId = request.getParameter("userId");
			String roleIdList = request.getParameter("roleIdList");
			String[] roleList = roleIdList.split(",");
			this.userService.saveRoleHasUser(roleList,userId,(Users) super.getUser());
		} catch (ServiceException se) {
			rm.setCode(se.getMessage());
			rm.setState("error");
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}

	@UrlDesc(value = "删除用户与角色权限")
	@RequestMapping(value ="/roleHasUsers/del")
	@ResponseBody
	public ResultModel roleHasUsersDel(RoleHasUsers roleHasUsers, Model model) {

		ResultModel rm = new ResultModel();
		rm.setState("success");
		try {
			this.userService.delRoleHasUser(roleHasUsers,
					(Users) super.getUser());
		} catch (ServiceException se) {
			rm.setCode(se.getMessage());
			rm.setState("error");
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}

	@UrlDesc(value = "用户与角色权限显示")
	@RequestMapping(value ="/roleHasUsers/query")
	@ResponseBody
	public ResultModel roleHasUsersGet(RoleHasUsers roleHasUsers, Model model) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			List<RoleHasUsers> list = this.userService.getRoleHasUser(roleHasUsers);
			rm.setData(list);
		} catch (ServiceException se) {
			rm.setCode(se.getMessage());
			rm.setState("error");
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}

	@UrlDesc(value = "添加用户与账户权限")
	@RequestMapping(value ="/accountHasUsers/add")
	@ResponseBody
	public ResultModel accountHasUsersAdd(HttpServletRequest request,
			Model model) {

		ResultModel rm = new ResultModel();
		rm.setState("success");
		AccountHasUsers au = new AccountHasUsers();
		try {
			String userId = request.getParameter("userId");
			String accountIdList = request.getParameter("accountIdList");
			String[] accountList = accountIdList.split(",");
			for (String accountId : accountList) {
				if (accountId.equals("")) {
				} else {
					au.setUserId(userId);
					au.setAccountId(accountId);
					this.userService.createAccountHasUser(au,
							(Users) super.getUser());
				}
			}
		} catch (ServiceException se) {
			rm.setCode(se.getMessage());
			rm.setState("error");
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}

	@UrlDesc(value = "删除用户与账户权限")
	@RequestMapping(value ="/accountHasUsers/del")
	@ResponseBody
	public ResultModel accountHasUsersDel(AccountHasUsers accountHasUsers,
			Model model) {

		ResultModel rm = new ResultModel();
		rm.setState("success");
		try {
			this.userService.delAccountHasUser(accountHasUsers,
					(Users) super.getUser());
		} catch (ServiceException se) {
			rm.setCode(se.getMessage());
			rm.setState("error");
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}

	@UrlDesc(value = "用户与账户权限显示")
	@RequestMapping(value ="/accountHasUsers/query")
	@ResponseBody
	public ResultModel accountHasUsersGet(AccountHasUsers accountHasUsers,
			Model model) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			List<AccountHasUsers> list = this.userService
					.getAccountHasUser(accountHasUsers);
			rm.setData(list);
		} catch (ServiceException se) {
			rm.setCode(se.getMessage());
			rm.setState("error");
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}
}
